package com.slkj.shilixiaoyuanapp.ui.parent.tool.schoolmuseum;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.WorksDetailsEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.schoolmuseum.StudentsWorksActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.view.MarginItemDecoration;
import com.slkj.shilixiaoyuanapp.view.MySwipeRefreshLayout;
import com.slkj.shilixiaoyuanapp.view.StateLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.PAGE_COUNT;

@ActivityInfo(layout = R.layout.activity_students_works)
public class StudentsWorksActivity extends BaseActivity {
    @InjectParam int type;
    @InjectParam String title;
    @BindView(R.id.recycler)
    RecyclerView recyclerView;
    @BindView(R.id.statelayout)
    StateLayout stateLayout;
    @BindView(R.id.swipeLayout)
    MySwipeRefreshLayout swipeRefreshLayout;
    private int page = 1;
    private int count = PAGE_COUNT;
    BaseQuickAdapter baseQuickAdapter;
    private List<WorksDetailsEntity> datas=new ArrayList<>();

    @Override
    protected void init() {
        //StudentsWorksActivity__JumpCenter.bind(this);
        setTitle(title);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new MarginItemDecoration(30));
        baseQuickAdapter=new BaseQuickAdapter<WorksDetailsEntity,BaseViewHolder>(R.layout.item_works_details,datas) {
            @Override
            protected void convert(BaseViewHolder helper, final WorksDetailsEntity item) {
                helper.setText(R.id.tv_studentname,item.getName()).setText(R.id.tv_workstime,item.getDate());
                Glide.with(StudentsWorksActivity.this).load(item.getImg()).asBitmap().centerCrop().into((ImageView) helper.getView(R.id.iv_worksbg));
            }
        };

        baseQuickAdapter.setOnLoadMoreListener(this::loadMore,recyclerView);
        recyclerView.setAdapter(baseQuickAdapter);
        refresh();
        swipeRefreshLayout.setOnRefreshListener(this::refresh);
        stateLayout.setOnReLoadListener(() -> refresh());
    }

    private void loadMore() {
        HttpHeper.get().toolService()
                .worksDetails(type,count, page,1)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<WorksDetailsEntity>>( ) {
                    @Override
                    public void onCallBackSuccess(List<WorksDetailsEntity> data) {
                        setData(false,data);
                    }

                    @Override
                    public void onFail(String mes) {
                        baseQuickAdapter.loadMoreFail();
                    }
                });
    }

    private void setData(boolean isRefresh, List<WorksDetailsEntity> data) {
        final int size = data == null ? 0 : data.size();
        page++;

        if (isRefresh) {
            baseQuickAdapter.setNewData(data);
        } else {
            if (size > 0) {
                baseQuickAdapter.addData(data);
            }
        }
        if (size < count) {
            //第一页如果不够一页就不显示没有更多数据布局
            baseQuickAdapter.loadMoreEnd(isRefresh);
        } else {
            baseQuickAdapter.loadMoreComplete();
        }
    }

    private void refresh() {
        page = 1;
        //这里的作用是防止下拉刷新的时候还可以上拉加载
        baseQuickAdapter.setEnableLoadMore(false);
        HttpHeper.get().toolService()
                .worksDetails(type,count, page,1)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<WorksDetailsEntity>>(stateLayout) {
                    @Override
                    public void onCallBackSuccess(List<WorksDetailsEntity> data) {
                        if (data.isEmpty()){
                            stateLayout.showEmptyView();
                        }
                        setData(true,data);
                        swipeRefreshLayout.setRefreshing(false);
                    }
                    @Override
                    public void onFail(String mes) {
                        super.onFail(mes);
                        swipeRefreshLayout.setRefreshing(false);

                    }
                });
    }
}
